//
//  WHYStatusCellTopView.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/25.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit



class WHYStatusCellTopView: UIView {
    /// StatusViewModel
    var viewModel: WHYStatusViewModel? {
        didSet {
//            QL2(viewModel?.thumbnailUrls)
            
            nameLabel.text = viewModel?.status.user?.screen_name
            
            iconView.sd_setImageWithURL((viewModel?.userProfileURL)!, placeholderImage: viewModel?.userDefaultImage)
            
            vipIconView.image = viewModel?.userVipImage

            memberIconView.image = viewModel?.userMemberImage
            
            timeLabel.text = viewModel?.createdTime
            
            sourceLabel.text = viewModel?.status.source
        }
    }
    
//    MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - 懒加载子控件
    /// 头像
    private var iconView: UIImageView = UIImageView(imageName: "avatar_default_big")
    /// 昵称
    private var nameLabel: UILabel = UILabel(title: "why")
    /// 会员图标
    private var memberIconView: UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    /// VIP图标
    private var vipIconView: UIImageView = UIImageView(imageName: "avatar_grassroot")
    /// 创建时间
    private var timeLabel: UILabel = UILabel(title: "刚刚",
        titleColor: UIColor.orangeColor(),
        fontSize: 11)
    /// 来源
    private var sourceLabel: UILabel = UILabel(title: "黑马微博", fontSize: 11)
    
}

// MARK: - 搭建UI
extension WHYStatusCellTopView {
    
    private func setupUI () {
        
        backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGrayColor()

        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(WHYStatusCellMagrin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(WHYStatusCellMagrin)
            make.top.equalTo(sepView.snp_bottom).offset(WHYStatusCellMagrin)
            make.size.equalTo(CGSizeMake(WHYStatusIconWidth, WHYStatusIconWidth))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(WHYStatusCellMagrin)
        }
        
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(WHYStatusCellMagrin)
        }
        
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(WHYStatusCellMagrin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(WHYStatusCellMagrin)
        }
        
    }
    
}
